import pygame
import time
from .sprite import Sprite  # 使用相对导入

class Storage:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.sprites = []  # 存储精灵列表
        self.eggs = []    # 存储精灵蛋
        # 添加UI相关初始化
        self.font = pygame.font.Font(None, 36)
        self.slot_size = 80
        self.slot_padding = 10
        self.slots_per_row = 4
        # 孵化按钮
        self.hatch_button = pygame.Rect(500, 400, 120, 40)
        
    def add_egg(self, sprite):
        """添加一个新的精灵蛋"""
        egg = Sprite(
            name=sprite.name,
            max_hp=sprite.max_hp,
            attack=sprite.attack,
            defense=sprite.defense,
            speed=sprite.speed
        )
        self.eggs.append((egg, None))  # None表示还未开始孵化
        
    def draw(self, screen):
        # 绘制精灵蛋格子
        for i, (sprite_type, hatch_time) in enumerate(self.eggs):
            row = i // self.slots_per_row
            col = i % self.slots_per_row
            x = 50 + col * (self.slot_size + self.slot_padding)
            y = 100 + row * (self.slot_size + self.slot_padding)
            
            # 绘制格子
            slot_rect = pygame.Rect(x, y, self.slot_size, self.slot_size)
            pygame.draw.rect(screen, (200, 200, 200), slot_rect)
            pygame.draw.rect(screen, (100, 100, 100), slot_rect, 2)
            
            # 绘制精灵蛋图标
            pygame.draw.ellipse(screen, (255, 223, 186), 
                              (x+20, y+20, self.slot_size-40, self.slot_size-40))
            
            # 显示孵化进度
            if hatch_time:
                elapsed = time.time() - hatch_time
                if elapsed < 10:
                    progress = f"{int((10-elapsed))}s"
                    text = self.font.render(progress, True, (0, 0, 0))
                    screen.blit(text, (x+30, y+30))
        
        # 绘制孵化按钮
        pygame.draw.rect(screen, (150, 150, 150), self.hatch_button)
        text = self.font.render("catch", True, (0, 0, 0))
        screen.blit(text, (self.hatch_button.x + 35, self.hatch_button.y + 10))
    
    def handle_click(self, pos):
        if self.hatch_button.collidepoint(pos):
            # 开始孵化所有未孵化的蛋
            current_time = time.time()
            for i, (sprite_type, hatch_time) in enumerate(self.eggs):
                if hatch_time is None:
                    self.eggs[i] = (sprite_type, current_time)
            return True
        return False
    
    def check_hatched_eggs(self):
        # 检查是否有蛋孵化完成
        current_time = time.time()
        hatched_sprites = []
        remaining_eggs = []
        
        for sprite_type, hatch_time in self.eggs:
            if hatch_time and (current_time - hatch_time >= 10):
                hatched_sprites.append(sprite_type)
            else:
                remaining_eggs.append((sprite_type, hatch_time))
        
        self.eggs = remaining_eggs
        return hatched_sprites
    
    def clear(self):
        """清空仓库中的所有精灵蛋"""
        self.eggs = []
        
    def start_hatching(self, sprite, hatch_time):
        """设置精灵蛋的孵化时间"""
        for i, (stored_sprite, _) in enumerate(self.eggs):
            if stored_sprite == sprite:
                self.eggs[i] = (sprite, hatch_time)
                break